package newcoder_exp.justPractise;
import java.util.*;
import org.junit.*;
public class NC48 {
    public class Solution {
        /* 
            在一个旋转数组中查找值
            较小的某一个部分移到后面
            [6,8,10,0,2,4],10
            2
        */
        public int search (int[] nums, int target) {
            int lo = 0, hi = nums.length - 1, mid, t = nums.length - 1;
            //1.确定查找的范围
            if (target > nums[0]) {
                //前半个区域
                while (lo <= hi) {
                    mid = (hi - lo) / 2 + lo;
                    if (nums[mid] < target && nums[mid] < nums[0] || nums[mid] > target) {
                        hi = mid - 1;
                    } else if (nums[mid] < target) {
                        lo = mid + 1;
                    } else {
                        return mid;
                    }
                }
            } else {
                //后半区域
                while (lo <= hi) {
                    mid = (hi - lo) / 2 + lo;
                    if (nums[mid] > target && nums[mid] < nums[t]) {
                        hi = mid - 1;
                    } else if (nums[mid] > target || nums[mid] < target) {
                        lo = mid + 1;
                    } else {
                        return mid;
                    }
                }
            }
            return -1;
        }
    }

    @Test
    public void test() {
        Solution s = new Solution();
        int[] nums = new int[]{6,8,10,0,2,4};
        int res = s.search(nums,7);
        System.out.println(res);
    }
}
